<?php

namespace App\Console\Commands\OpenShop;

use App\Enums\OpenShop\InvestmentPromotion\SignOrderEnum;
use App\Enums\OpenShop\InvestmentPromotion\SiteMissionEnum;
use App\Enums\OpenShop\MissionTemplateEnum;
use App\Models\OpenShop\InvestmentPromotion\OpenShopMainProcessModel;
use App\Models\OpenShop\InvestmentPromotion\OpenShopSignOrderModel;
use App\Models\OpenShop\InvestmentPromotion\OpenShopSiteMissionModel;
use App\Service\OpenShop\Feishu\MissionTemplateService;
use Illuminate\Console\Command;

class RefreshNowStepStatus extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'openShop:refresh-nowStepStatus {orderNo?}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '更新main_process表nowStepStatus';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return int
     */

    private $errs;

    public function handle()
    {
        $argOrderNo = $this->argument('orderNo');

        OpenShopSignOrderModel::query()
                              ->when($argOrderNo, function ($query) use ($argOrderNo) {
                                  $query->where('orderNo', $argOrderNo);
                              })
                              ->where('status', '!=', SignOrderEnum::ORDER_STATUS_NO)
                              ->get()
                              ->each(function ($item) {

                                  $f = function () use ($item) {

                                      $siteMissions = OpenShopSiteMissionModel::query()
                                                                              ->where('orderNo', $item->orderNo)
                                                                              ->select('stepStatus', 'status')->get();

                                      if ($siteMissions->where('stepStatus', MissionTemplateEnum::STEP_STATUS_18)
                                                       ->where('status', SiteMissionEnum::STATUS_2)
                                                       ->count()) {
                                          return MissionTemplateEnum::STEP_STATUS_999;
                                      }

                                      if (!$siteMissions->count()) {

                                          // 根据sign_order状态来
                                          if ($item->step == SignOrderEnum::STEP_1) {
                                              $stepStatus = MissionTemplateEnum::STEP_STATUS_0;
                                          } elseif ($item->step == SignOrderEnum::STEP_2 || $item->step == SignOrderEnum::STEP_3) {
                                              $stepStatus = MissionTemplateEnum::STEP_STATUS_1;
                                          } elseif ($item->step == SignOrderEnum::STEP_4) {
                                              $stepStatus = MissionTemplateEnum::STEP_STATUS_2;
                                          } elseif ($item->step == SignOrderEnum::STEP_5) {
                                              $stepStatus = MissionTemplateEnum::STEP_STATUS_3;

                                              // 异常订单
                                              if (config('app.env') == 'qa' || config('app.env') == 'dev') {
                                                  $item->update(['status' => SignOrderEnum::ORDER_STATUS_NO]);
                                              } else {
                                                  $this->errs .= $item->orderNo . PHP_EOL;
                                              }

                                          } else {
                                              $stepStatus = 0;
                                          }

                                          return $stepStatus;
                                      }

                                      $ingMission = OpenShopSiteMissionModel::query()->where('orderNo', $item->orderNo)
                                                                            ->where('status', SiteMissionEnum::STATUS_1)
                                                                            ->count();
                                      if (!$ingMission) {
                                          $finish = OpenShopSiteMissionModel::query()->where('orderNo', $item->orderNo)
                                                                            ->where('stepStatus',
                                                                                MissionTemplateEnum::STEP_STATUS_18)
                                                                            ->count();
                                          if ($finish) {
                                              return MissionTemplateEnum::STEP_STATUS_100;
                                          } else {

                                              // 异常订单
                                              if (config('app.env') == 'qa' || config('app.env') == 'dev') {
                                                  $item->update(['status' => SignOrderEnum::ORDER_STATUS_NO]);
                                              } else {
                                                  $this->errs .= $item->orderNo . PHP_EOL;
                                              }
                                          }
                                      }

                                      $maxStepStatus = 0;
                                      $maxSort       = 0;
                                      $siteMissions->each(function ($site) use (&$maxStepStatus, &$maxSort) {
                                          $sort = MissionTemplateService::getSortByStepStatus($site['stepStatus']);
                                          if ($sort > $maxSort) {
                                              $maxStepStatus = $site['stepStatus'];
                                              $maxSort       = $sort;
                                          }
                                      });

                                      return $maxStepStatus;
                                  };


                                  $stepStatus = $f();

                                  $res = OpenShopMainProcessModel::updateNowStepStatus($item->orderNo, $stepStatus);

                                  if ($res) {
                                      $this->info($item->orderNo . '..' . $stepStatus);
                                  }

                                  if ($stepStatus == MissionTemplateEnum::STEP_STATUS_999 && $item->status != SignOrderEnum::ORDER_STATUS_COMPLETE) {
                                      $item->status = SignOrderEnum::ORDER_STATUS_COMPLETE;
                                      $item->save();
                                      $this->info($item->orderNo . '..status complete');
                                  }

                                  if ($this->errs) {
                                      $this->info('--------------------------');
                                      $this->info('任务终端的订单:');
                                      $this->info($this->errs);
                                  }
                              });
    }
}
